package com.yeswater.algorithm.leetcode.hot100;

public class Code5 {

    public static void main(String[] args) {
        String s = "babad";
        Code5 code5 = new Code5();
        System.out.println(code5.longestPalindrome(s));
    }

    public String longestPalindrome(String s) {

        String ans = "";
        for(int i=0; i<s.length(); i++){
            // 奇回文
            int l = i-1;
            int r = i+1;
            while(l >= 0 && r <s.length() && s.charAt(l) == s.charAt(r)){
                l--;
                r++;
            }
            if (ans.length() < r - l - 1){
                // substring 左闭右开
                ans = s.substring(l+1, r);
            }

            // 偶回文
            l = i;
            r = i+1;
            while(l >= 0 && r <s.length() && s.charAt(l) == s.charAt(r)){
                l--;
                r++;
            }
            if (ans.length() < r - l - 1){
                // substring 左闭右开
                ans = s.substring(l+1, r);
            }

        }

        return ans;
    }

}
